package com.example.myapplication6;

import androidx.appcompat.app.AppCompatActivity;
import java.io.OutputStream;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    public Handler handler;
    EditText et_main_content;
    TextView tv_receive_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        Button bn,bn2;
        et_main_content=(EditText)findViewById(R.id.et_main_content);
        tv_receive_content=(TextView)findViewById(R.id.tv_receive_content);
        bn=(Button)findViewById(R.id.btn_main_send);
        bn2=(Button)findViewById(R.id.btn_main_connect);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0x1234){
                    tv_receive_content.setText(tv_receive_content.getText().toString()+msg.getData().getString("receive_content"));
                }
            }
        };
        bn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg=new Message();
                msg.what=0x1234;
                Bundle bundle=new Bundle();
                bundle.putString("send_content",et_main_content.getText().toString()+"\n");
                msg.setData(bundle);
                ClientThread.SendHandler.sendMessage(msg);
            }
        });
        bn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    new Thread(new ClientThread(handler)).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
class ClientThread implements  Runnable{
    public static Handler SendHandler=null;
    Socket sk=null;
    Handler RecHandler=null;
    BufferedReader br=null;
    OutputStream os=null;
    public ClientThread(Handler handler) throws IOException {
        this.RecHandler=handler;
    }
    @Override
    public void run() {
        try {
            sk=new Socket("127.0.0.1",30000);
            br=new BufferedReader(new InputStreamReader(sk.getInputStream()));
            os=sk.getOutputStream();
            new Thread(){
                @Override
                public void run() {
                    String content=null;
                    try {
                        while ((content = br.readLine()) != null) {
                            Message msg = new Message();
                            msg.what = 0x1234;
                            Bundle bundle = new Bundle();
                            bundle.putString("receive_content", content);
                            msg.setData(bundle);
                            RecHandler.sendMessage(msg);

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Looper.prepare();
        SendHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0x1234){
                    try {
                        os.write(msg.getData().getString("send_content").getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Looper.loop();

    }
}